package com.lushu.chapitre1.engine
{
    import com.lushu.chapitre1.events.MenuEvent;
    import com.lushu.chapitre1.graphics.controls.BattleInformation;
    import com.lushu.chapitre1.graphics.controls.ConfigurableMenu;
    import com.sfdk.graphics.core.SpriteEx;
    import com.sfdk.graphics.engine.data.CharacterDefinition;

    import flash.events.Event;

    public class MenuEngine extends SpriteEx
    {
        private var _scale : int;
        private var _configurableMenu : ConfigurableMenu;
        private var _battleInformation : BattleInformation;
        public var hasOpenMenu : Boolean;
        public function MenuEngine(scale : int)
        {
            _scale = scale;

            init();
        }

        private function init() : void
        {
            _configurableMenu = new ConfigurableMenu(_scale);
            var menuConfiguration : Vector.<Object> = new <Object>[ConfigurableMenu.MAGIC_BTN, ConfigurableMenu.ATTACK_BTN, ConfigurableMenu.ITEM_BTN, ConfigurableMenu.STAY_BTN];
            _configurableMenu.configure(menuConfiguration, "STAY_BTN");
            _configurableMenu.hide();

            this.attachChild(_configurableMenu, 176, 448, "configurableMenu");


            _battleInformation = new BattleInformation();
            this.attachChild(_battleInformation, 16, 14, "battleInformation");
        }

        public function update() : void
        {
            _configurableMenu.update();
            _battleInformation.update();
        }

        public function setCurrentCharacter(characterDefinition : CharacterDefinition) : void
        {
            _battleInformation.setCurrentCharacter(characterDefinition);
        }

        public function setCurrentEnemy(characterDefinition : CharacterDefinition) : void
        {
            _battleInformation.setCurrentEnemy(characterDefinition);
        }

        public function setCurrentTileInfo(tileInfo : int) : void
        {
            _battleInformation.setCurrentTileInfo(tileInfo);
        }

        public function setDefaultAction(actionName : String):void
        {
            _configurableMenu.setTargetButton(actionName);
        }

        public function handleArrowKeys(e : Event) : Boolean
        {
            var hasMoved : Boolean = false;

            if(_configurableMenu.visible)
            {
                _configurableMenu.handleArrowKeys(e);
                hasMoved = true;
            }

            return hasMoved;
        }

        public function handleEnterKey(e : Event) : Boolean
        {
            var hasMoved : Boolean = false;
            if(_configurableMenu.visible)
            {
                _configurableMenu.handleEnterKey(e);
                hasMoved = true;
            }

            return hasMoved;
        }

        public function handleSpaceBar(e : Event) : void
        {
            if(_configurableMenu.visible)
            {
                _configurableMenu.hide();
                hasOpenMenu = false;
            }
            else
            {
                _configurableMenu.show();
                hasOpenMenu = true;
                dispatchEvent(new MenuEvent(MenuEvent.SHOW_MENU))
            }

        }

//        private function onSearchAction(event:Event):void {
//            if (!_textBox) {
//                _battleMenu.setTargetY(448, true);
//                var textsToDisplay:String = LocalisedMessageEngine.getMessage("searchPhrase") + "\n" + LocalisedMessageEngine.getMessage("searchNothingFound");
//                _textBox = new TextBox("");
//                var initX:int = int(_currentCharacter.x / (24));
//                var initY:int = int(_currentCharacter.y / (24));
//                var direction:String = _currentCharacter.getDirection();
//                switch (direction) {
//                    case "Droite":
//                        initX++;
//                        break;
//
//                    case "Gauche":
//                        initX--;
//                        break;
//
//                    case "Dos":
//                        initY--;
//                        break;
//
//                    case "Face":
//                        initY++;
//                        break;
//                }
//                _textBox.addEventListener(NPCEvent.END, function(e:Event):void {
//                    _mainWindow.removeChild(_textBox);
//                    _textBox = null;
//                    _inMenu = false
//                })
//                if ((initX == _npcSprite.movementInitX && initY == _npcSprite.movementInitY) || (initX == _npcSprite.movementDestX && initY == _npcSprite.movementDestY)) {
//                    _dialogueDescriptor = new DialogueDescriptor(_textBox, "resources/npc/" + _npcSpriteDialogue);
//                }
//                else {
//                    _textBox.setContentText(textsToDisplay, false);
//                }
//
//                _mainWindow.attachChild(_textBox, 16, 306);
//            }
//        }

        public function switchToBattleMode():void
        {
            _battleInformation.hideLandEffect();
            _battleInformation.moveEnemyInformation(0, 306);
        }

        public function switchToMoveMode():void
        {
            _battleInformation.setCurrentEnemy(null);
            _battleInformation.showLandEffect();
            _battleInformation.moveEnemyInformation(288, 330);
        }
    }
}
